﻿//需要先添加
//Microsoft.EntityFrameworkCore
//Microsoft.EntityFrameworkCore.Sqlite
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

public class School
{
    public int SchoolId { get; set; }
    public string Name { get; set; }
    public List<Class> Classes { get; set; } = new List<Class>();
}

public class Class
{
    public int ClassId { get; set; }
    public string Name { get; set; }
    public int SchoolId { get; set; }
    public School School { get; set; }
    public List<Student> Students { get; set; } = new List<Student>();
}

public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public int ClassId { get; set; }
    public Class Class { get; set; }
}

public class Log
{
    public int LogId { get; set; }
    public DateTime Date { get; set; }
    public string Operation { get; set; }
    public string Details { get; set; }
}

public class SchoolContext : DbContext
{
    public DbSet<School> Schools { get; set; }
    public DbSet<Class> Classes { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<Log> Logs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source=school.db");
    }
}

public class DataOperations
{
    private readonly SchoolContext _context;

    public DataOperations(SchoolContext context)
    {
        _context = context;
    }

    public async Task AddSchoolAsync(string name)
    {
        var school = new School { Name = name };
        _context.Schools.Add(school);
        await _context.SaveChangesAsync();

        AddLogAsync("Add School", $"Added {school.Name}");
    }

    public async Task AddClassAsync(string name, int schoolId)
    {
        var school = await _context.Schools.FindAsync(schoolId);
        if (school == null) throw new KeyNotFoundException("School not found.");

        var @class = new Class { Name = name, School = school };
        _context.Classes.Add(@class);
        await _context.SaveChangesAsync();

        AddLogAsync("Add Class", $"Added {name} to {school.Name}");
    }

    public async Task AddStudentAsync(string name, int age, int classId)
    {
        var @class = await _context.Classes.FindAsync(classId);
        if (@class == null) throw new KeyNotFoundException("Class not found.");

        var student = new Student { Name = name, Age = age, Class = @class };
        _context.Students.Add(student);
        await _context.SaveChangesAsync();

        AddLogAsync("Add Student", $"Added {name} to {@class.Name}");
    }

    public async Task AddLogAsync(string operation, string details)
    {
        var log = new Log { Date = DateTime.Now, Operation = operation, Details = details };
        _context.Logs.Add(log);
        await _context.SaveChangesAsync();
    }

    // 这里可以添加更多的数据操作方法，例如查询、更新和删除
}

class Program
{
    static async Task Main(string[] args)
    {
        using (var context = new SchoolContext())
        {
            context.Database.EnsureCreated(); // 确保数据库和表已创建

            var dataOps = new DataOperations(context);

            // 添加学校和班级
            await dataOps.AddSchoolAsync("示例学校");
            await dataOps.AddClassAsync("一班", 1);
            await dataOps.AddClassAsync("二班", 1);

            // 添加学生
            await dataOps.AddStudentAsync("张三", 18, 1);
            await dataOps.AddStudentAsync("李四", 17, 1);
            await dataOps.AddStudentAsync("王五", 16, 2);

            Console.WriteLine("数据初始化完成。");
        }
    }
}